package de.bazinga.rotw.objects;

import java.net.URL;

import com.jme.bounding.BoundingBox;
import com.jme.math.Vector3f;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Box;
import com.jmex.game.state.GameStateManager;

import de.bazinga.rotw.collision.CollidableObjects;
import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.network.BasicSyncManager;
import de.bazinga.rotw.network.Client;
import de.bazinga.rotw.network.Server;
import de.bazinga.rotw.network.message.ItemMessage;
import de.bazinga.rotw.person.Person;
import de.bazinga.rotw.texture.TextureCache;

public class TwoButtons extends Button {
	private static final long serialVersionUID = 1L;
	
	
	private Box buttonTwo = null;
	
	public TwoButtons(Vector3f buttonOneMin, Vector3f buttonOneMax,
			Vector3f buttonTwoMin, Vector3f buttonTwoMax, Vector3f wallMin,
			Vector3f wallMax, long openDuration, boolean hideOnCollision, 
			int team, URL buttonTexture, URL wallTexture) {
		
		super(buttonOneMin, buttonOneMax, wallMin, wallMax, openDuration, hideOnCollision,
				team, buttonTexture, wallTexture);
		
		buttonTwo = new Box("button", buttonTwoMin, buttonTwoMax);
		
		buttonTwo.setLightCombineMode(Spatial.LightCombineMode.Off);
		
		buttonTwo.setModelBound(new BoundingBox());
		buttonTwo.updateModelBound();
		
		if(buttonTexture != null) {
			buttonTwo.setRenderState(TextureCache.getInstance().getTexture(buttonTexture));
		}
		buttonTwo.updateRenderState();
		
		this.attachChild(buttonTwo);
	}


	public void onCollision(Person person, boolean sendMessage) {
		super.onCollision(person, sendMessage);
		
		if(team == 1 && person.getTeam() == Person.TEAM_TWO) return ;
		if(team == 2 && person.getTeam() == Person.TEAM_ONE) return ;
		
		if(person.hasCollision(buttonTwo, false)) {
			// Tell Server that we updated
			if(sendMessage) {
				ItemMessage itemMsg = new ItemMessage();
				itemMsg.setItem(ItemMessage.ITEM_BUTTON);
				itemMsg.setItemIndex(CollidableObjects.getInstance().getObjects("gameObjects").indexOf(this));
				itemMsg.setHide(true);
				
				BasicSyncManager syncManager = 
					((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getSyncManager();
				if(syncManager instanceof Client) {
					((Client) syncManager).getClient().sendToServer(itemMsg);
				}else{
					((Server) syncManager).getServer().sendToAll(itemMsg);
				}
			}
			hide();
		}
	}
}